
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "vgui/cl_blockplacer.lua")
AddCSLuaFile( "vgui/cl_teamSelect.lua" )
AddCSLuaFile( "vgui/cl_scoreboard.lua" )
AddCSLuaFile( "vgui/cl_voice.lua" )
AddCSLuaFile( "vgui/cl_hud.lua" )
AddCSLuaFile( "shared.lua" )

include( "shared.lua" )
include( "sv_rounds.lua" )
include( "mapvote/sv_mapvote.lua" )


--Edit all your round times here.. NOWHERE ELSE!!!!!!! Or, I kill you (: <3
SetGlobalVar( "hns_freezetime_seconds", 20 )

freezetime = GetGlobalVar( "hns_freezetime_seconds" )

function SetHider(ply)
	ply:SetTeam( TEAM_HIDER)
	ply:Kill()
	ply:ChatPrint("You have been set team Hiders!" )
end
	
function SetSeeker(ply)
ply:SetTeam( TEAM_SEEKER)
	ply:Kill()
	ply:ChatPrint ("You have been set team Seekers!" )
end
	
function SetBuilder(ply)
ply:SetTeam( TEAM_BUILDER)
	ply:Kill()
	ply:ChatPrint ("You have been set team Builder!" )
end
	
function SetSpec(ply)
	ply:SetTeam( TEAM_SPECTATOR)
	ply:Kill()
	ply:ChatPrint ("You have been set to spectator!" )
end
concommand.Add( "SetHider", SetHider )
concommand.Add( "SetSeeker", SetSeeker )
concommand.Add( "SetBuilder", SetBuilder )
concommand.Add( "SetSpec", SetSpec )

function GM:Initialize()

	timer.Create( "TimeWarp", 1, 0, RoundTimer )
	
	local filename = file.Read( "hide_n_seek/" .. game.GetMap() .. ".txt", "DATA" ) 
	local thetable = util.JSONToTable( filename )
	
	for k, v in ipairs( thetable ) do
		newEnt = ents.Create( v.class )
		newEnt:SetPos( v.pos )
		newEnt:SetModel( "models/hunter/blocks/cube1x1x025.mdl" )
		newEnt:Spawn()
		newEnt:Activate()
	end
	
end

function playersAliveOnTeam(t)
	local i = 0
	for _,v in pairs(team.GetPlayers(t)) do
		if v:Alive() then i = i + 1 end
	end

	return i
end



-----------------IGNORE ANYTHING BELOW THIS---------------------------
-----------------IGNORE ANYTHING BELOW THIS---------------------------
-----------------IGNORE ANYTHING BELOW THIS---------------------------
-----------------IGNORE ANYTHING BELOW THIS---------------------------
-----------------IGNORE ANYTHING BELOW THIS---------------------------
-----------------IGNORE ANYTHING BELOW THIS---------------------------
-----------------IGNORE ANYTHING BELOW THIS---------------------------


function GM:PlayerInitialSpawn(ply)

local teams = {TEAM_SEEKER, TEAM_HIDER}
local seek = team.NumPlayers(TEAM_SEEKER)
local hide = team.NumPlayers(TEAM_HIDER)

	if seek == hide then
		ply:SetTeam(table.Random(teams))
	elseif seek > hide then
		ply:SetTeam(TEAM_HIDER)
		print("YOU HAVE BEEN SET TO TEAM HIDER")
	elseif seek < hide then
		ply:SetTeam(TEAM_SEEKER)
		print("YOU HAVE BEEN SET TO TEAM SEEKER")
	end

	print(ply:Nick() .. "Is on " .. ply:Team())
end


SeekerModels = {
	"models/player/leet.mdl",
	"models/player/guerilla.mdl",
	"models/player/arctic.mdl",
	"models/player/phoenix.mdl"
}
 
HiderModels = {
	"models/player/urban.mdl",
	"models/player/gasmask.mdl",
	"models/player/riot.mdl",
	"models/player/swat.mdl"
}

Round_Phase = GetGlobalVar( "RoundPhase" )
function GM:PlayerSpawn( ply )
	self.BaseClass:PlayerSpawn( ply )
	ply:SetHealth(ply:GetMaxHealth())
	ply:SetHull( Vector( -16, -16, 0 ), Vector( 16, 16, 62 ) )
	ply:SetHullDuck( Vector( -16, -16, 0 ), Vector( 16, 16, 40 ) )
	ply:SetArmor(100)
	ply:AllowFlashlight( true )
	ply:SetJumpPower(225)
	ply:SetNoCollideWithTeammates( true )
	
	local col = nil

	if ply:Team() == TEAM_HIDER then
		col = team.GetColor(TEAM_HIDER)
		ply:SetModel( table.Random (HiderModels) )
		ply:SetAnimation( ACT_MP_RUN )
	elseif ply:Team() == TEAM_SEEKER then
		col = team.GetColor(TEAM_SEEKER)
		ply:SetModel( table.Random (SeekerModels) )
	elseif ply:Team() == TEAM_BUILDER then
		col = team.GetColor(TEAM_BUILDER)
		ply:SetModel( "models/player/gman_high.mdl" )
		ply:SetRenderMode( RENDERMODE_TRANSALPHA )
		ply:SetColor( Color(255, 255, 255, 150) )    --We will make him look like a ghost =D
	elseif ply:Team() == TEAM_SPECTATOR then
		col = team.GetColor(TEAM_SPECTATOR)
	end

	local spawnpoints = ents.FindByClass( ply:Team() == TEAM_SEEKER and "info_player_counterterrorist" or "info_player_terrorist" )

	if #spawnpoints > 0 then
		local position = table.Random( spawnpoints ):GetPos()
		ply:SetPos( position )

		timer.Simple( 1, function()
			if (IsValid(ply) and ply:Alive() and position) then
				ply:SetPos( position )
			end
		end )
	end
	
	local mdl = hook.Call( "ChangePlayerModel", GAMEMODE, ply ) or false
	
	umsg.Start("spawnsound", ply)
	umsg.End()
	
	--		if Round_Phase == 0 then
	--	ply:Freeze(true)
	--	end
	
end

function GM:PlayerLoadout(ply)
	ply:StripWeapons()
	ply:StripAmmo()
	if ply:Team() == TEAM_SEEKER then
		ply:SetWalkSpeed( 250 )
		ply:SetRunSpeed ( 125 )
		ply:Give( "weapon_crowbar" )
	elseif ply:Team()== TEAM_HIDER then
		ply:SetWalkSpeed( 250 )
		ply:SetRunSpeed ( 125 )
	elseif ply:Team()== TEAM_BUILDER then
		ply:Give( "weapon_physgun" )
	end 
end

function GM:ShowHelp(ply) 
	if not IsValid(ply) then return end

	umsg.Start("F1Menu", ply)
	umsg.End()
end

function GM:ShowTeam(ply)
	if not IsValid(ply) then return end
	
	umsg.Start("BlockPlacer", ply)
	umsg.End()
end


concommand.Add("test_write", function()
	if ( !file.IsDir( "hide_n_seek", "DATA" ) ) then file.CreateDir( "hide_n_seek" ) end 

	local BlockPositions = {}
	for k, v in pairs( ents.FindByModel( "models/hunter/blocks/cube1x1x025.mdl") ) do
		table.insert( BlockPositions, { class = v:GetClass(), pos = v:GetPos() } )
	end

	file.Write( "hide_n_seek/" .. game.GetMap()  .. ".txt", util.TableToJSON( BlockPositions ) )
end )


concommand.Add ("test_spawn", function()
	local filename = file.Read( "hide_n_seek/" .. game.GetMap() .. ".txt", "DATA" ) 
	local thetable = util.JSONToTable( filename )
	
	for k, v in ipairs( thetable ) do
		newEnt = ents.Create( v.class )
		newEnt:SetPos( v.pos )
		newEnt:SetModel( "models/hunter/blocks/cube1x1x025.mdl" )
		newEnt:Spawn()
		newEnt:Activate()
	end
end)


resource.AddFile("resource/fonts/DigitFont.ttf")
resource.AddFile("resource/fonts/coolvetica.ttf")
resource.AddFile("materials/halflife/50cal.vmt")
resource.AddFile("materials/halflife/338cal.vmt")
resource.AddFile("materials/halflife/armor.vmt")
resource.AddFile("materials/halflife/clock.vmt")
resource.AddFile("materials/halflife/fb.vmt")
resource.AddFile("materials/halflife/fg.vmt")
resource.AddFile("materials/halflife/flash-on.vmt")
resource.AddFile("materials/halflife/flash-off.vmt")
resource.AddFile("materials/halflife/he.vmt")
resource.AddFile("materials/halflife/hp_cross.vmt")
resource.AddFile("materials/halflife/voiceicon.vmt")
